package ${domain}.module.wsys.mgr;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import ${domain}.frame.base.ErrorType;
import ${domain}.frame.auth.Token;
import ${domain}.frame.utils.IDgenerator;
import ${domain}.frame.utils.MapperUtil;
import ${domain}.frame.utils.Message;
import ${domain}.frame.utils.ValidationUtil;
import ${domain}.module.wsys.ent.DictItem;
import ${domain}.module.wsys.mpr.DictItemMapper;
import ${domain}.module.wsys.req.DictItemCreateRequest;
import ${domain}.module.wsys.req.DictItemDeleteRequest;
import ${domain}.module.wsys.req.DictItemFindRequest;
import ${domain}.module.wsys.req.DictItemUpdateRequest;
import ${domain}.module.wsys.rsp.DictItemCreateResponse;
import ${domain}.module.wsys.rsp.DictItemDeleteResponse;
import ${domain}.module.wsys.rsp.DictItemFindResponse;
import ${domain}.module.wsys.rsp.DictItemUpdateResponse;

/**
 * DICT_ITEM - 字典项
 *
 * @author wangbing
 * @version 0.0.1
 * @since 2017-01-01
 */
@Transactional
@Service
public class DictItemManagerImpl implements DictItemManager {

    @Autowired
    private DictItemMapper dictItemMapper;

    /**
     * 插入
     *
     * @param request 请求对象
     * @param token   令牌
     * @return 响应
     */
    public DictItemCreateResponse create(DictItemCreateRequest request, Token token) {
        DictItemCreateResponse response = new DictItemCreateResponse();

        ValidationUtil.validate(request, response);
        if (response.hasError()) {
            return response;
        }

        {// 检查字典项是否重复
            DictItemFindRequest dictItemFindRequest = new DictItemFindRequest();
            dictItemFindRequest.setDictName(request.getDictName());
            dictItemFindRequest.setKey(request.getKey());
            dictItemFindRequest.setValid(true);
            DictItemFindResponse dictItemFindResponse = this.find(dictItemFindRequest, token);
            if (dictItemFindResponse.hasError()) {
                response.addErrors(dictItemFindResponse.getErrors());
                return response;
            } else if (dictItemFindResponse.getTotalCount() > 0) {
                response.addError(ErrorType.BUSINESS_ERROR, "字典项重复");
                return response;
            }
        }

        long id = IDgenerator.nextId();
        DictItem entity = MapperUtil.map(request, DictItem.class);
        entity.setId(id);

        long result = dictItemMapper.insert(entity, token);
        if (1L != result) {
            response.addError(ErrorType.BUSINESS_ERROR, Message.CREATE_FAILURE);
            return response;
        }
        response.setId(id);

        return response;
    }

    /**
     * 逻辑删除
     *
     * @param request 请求对象
     * @param token   令牌
     * @return 响应
     */
    public DictItemDeleteResponse delete(DictItemDeleteRequest request, Token token) {
        DictItemDeleteResponse response = new DictItemDeleteResponse();

        ValidationUtil.validate(request, response);
        if (response.hasError()) {
            return response;
        }

        long result = dictItemMapper.delete(request.getId(), token);
        if (1L != result) {
            response.addError(ErrorType.BUSINESS_ERROR, Message.DELETE_FAILURE);
            return response;
        }
        response.setResult(result);

        return response;
    }

    /**
     * 更新
     *
     * @param request 请求对象
     * @param token   令牌
     * @return 响应
     */
    public DictItemUpdateResponse update(DictItemUpdateRequest request, Token token) {
        DictItemUpdateResponse response = new DictItemUpdateResponse();

        ValidationUtil.validate(request, response);
        if (response.hasError()) {
            return response;
        }

        DictItem entity = dictItemMapper.getById(request.getId(), token);
        if (entity == null) {
            response.addError(ErrorType.BUSINESS_ERROR, Message.GET_FAILURE);
            return response;
        }

        MapperUtil.map(request, entity);
        long result = dictItemMapper.update(entity, token);
        if (1L != result) {
            response.addError(ErrorType.BUSINESS_ERROR, Message.UPDATE_FAILURE);
            return response;
        }
        response.setResult(result);

        return response;
    }

    /**
     * 查询
     *
     * @param request 请求对象
     * @param token   令牌
     * @return 响应
     */
    @Transactional(readOnly = true)
    public DictItemFindResponse find(DictItemFindRequest request, Token token) {
        DictItemFindResponse response = new DictItemFindResponse();

        ValidationUtil.validate(request, response);
        if (response.hasError()) {
            return response;
        }

        if (request.getPageSize() != 0) {
            PageHelper.startPage(request.getPageNumber(), request.getPageSize());
        }
        if (StringUtil.isNotEmpty(request.getSortKey())) {
            PageHelper.orderBy(request.getSortKey() + " " + request.getSortType());
        }
        PageInfo<DictItem> pageInfo = new PageInfo<>(dictItemMapper.find(request, token));

        response.setResult(pageInfo.getList());
        response.setTotalCount(pageInfo.getTotal());

        return response;
    }
}
